'''
Created on 28/09/2011

@author: Ike-kun <ragnell22@gmail.com>
'''

class LogManager(object):
    def __init__(self):
        self.format = None
        self.target = None
        self.timeFormat = None
        self.logLevel = None
        self.confData = []
        self.readConf()

    def readConf(self):
        try:
            file = open("log4py.conf","rb")
            for row in file:
                if(row.startswith('Format:')):
                    self.setFormat(row.split(':')[1].strip())
                if(row.startswith('Target:')):
                    self.setTarget(row.split(':')[1].strip())
                if(row.startswith('TimeFormat:')):
                    self.setTimeFormat(row.split(':')[1].strip())
                if(row.startswith('LogLevel:')):
                    self.setLogLevel(row.split(':')[1].strip())
                self.confData.append(row)
            file.flush()
            file.close()
            return True
        except IOError as (errno, strerror):
            return False

    def saveConf(self):
        try:
            file = open("log4py.conf","w")
            for row in self.confData:
                if(row.startswith('Format:')):
                    row = row.replace(row.split(':')[1],self.format)
                if(row.startswith('Target:')):
                    row = row.replace(row.split(':')[1],self.target)
                if(row.startswith('TimeFormat:')):
                    row = row.replace(row.split(':')[1],self.timeFormat)
                if(row.startswith('LogLevel:')):
                    row = row.replace(row.split(':')[1],self.logLevel)
                file.write(row.strip()+"\n")
            file.flush()
            file.close()
            return True
        except IOError as (errno, strerror):
            return False

    def getFormat(self):
        return self.format

    def setFormat(self,format):
        self.format = format

    def getTarget(self):
        return self.target

    def setTarget(self,target):
        self.target = target

    def getTimeFormat(self):
        return self.timeFormat

    def setTimeFormat(self,timeFormat):
        self.timeFormat = timeFormat

    def getLogLevel(self):
        return self.logLevel

    def setLogLevel(self,logLevel):
        self.logLevel = logLevel

    def getEverything(self):
        return [self.format,self.target,self.timeFormat,self.logLevel]
